package com.lixin.juc.blockingqueue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

// 阻塞队列-同步队列
public class MySynchronousQueue {

    public static void main(String[] args) {
        BlockingQueue<String> queue = new SynchronousQueue<>();

        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName() + "开始放one");
                queue.put("one");
                System.out.println(Thread.currentThread().getName() + "开始放two");
                queue.put("two");
                System.out.println(Thread.currentThread().getName() + "开始放three");
                queue.put("three");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "AThread").start();

        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName() + "取出" + queue.take());
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName() + "取出" + queue.take());
                TimeUnit.SECONDS.sleep(2);
                System.out.println(Thread.currentThread().getName() + "取出" + queue.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "BThread").start();
    }

}
